<?php

namespace app\mobile\controller;

use app\mobile\common\Base;
use think\Db;
use think\Session;
//保修激活
class Activation extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $is_sn=is_sn($data['sn']);
        $this->assign('real_sn',$is_sn['sn']);
        return $this->fetch();
    }

    public function active()
    {
        $status = 0;
        $msg = "失败";
        $data = $this->request->param();
        $tg = $this->getPro();
        $is_a = Db::name('card')->where('tagslogs_sn', $tg['sn'])->where('checkstatus', 'in', [0, 1])->count();
        if ($is_a > 0) {
            $msg = "提交失败，已经存在激活信息";
        } else {
            $insert = [
                'tagslogs_sn' => $tg['sn'],
                'danwei' => $data['companyName'],
                'user' => $data['name'],
                'phone' => $data['companyPhone'],
                'mobile_phone' => $data['phone'],
                'email' => $data['email'],
                'addtime' => time(),
                'updatetime' => time(),
                'checkstatus' => 0,
                'bianhao' => bh(),
                'province' => getlocation(request()->ip())['province'],
                'ip' => request()->ip(),
            ];
            $res = Db::name('card')->insert($insert);

            if (!empty($res)) {
                // 发送审核短信到用户
                $msg_temp=get_msg_temp(1);
                if($msg_temp['is_on']==1){
                    if($msg_temp['type']==1||$msg_temp['type']==2){//通知短信
                        send_mobile_message($data['phone'], "shenhe",$tg['sn']);
                    }
                    if($msg_temp['type']==1||$msg_temp['type']==3){//通知邮箱
                        if(!empty($data['email'])){
                            //sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
                            sendToMail($data['email'], $msg_temp['email_title'], $msg_temp['email_content']);
                        }

                    }
                }
               
                // 查询所有客服人员
                $kfs = Db::name('admin')->where(['status'=>1, 'is_kf'=>1])->select();
                $msg_temp1=get_msg_temp(2);
                foreach ($kfs as $key => $value) {
                    // 给客服人员发短信提醒

                    if($msg_temp1['is_on']==1){
                        if($msg_temp1['type']==1||$msg_temp1['type']==2){//通知短信
                            if(!empty($value['telephone'])){
                                send_mobile_message($value['telephone'], "kfmsg",$tg['sn']);
                            }
                        }
                        if($msg_temp1['type']==1||$msg_temp1['type']==3){//通知邮箱
                            if(!empty($value['email'])){
                                //sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
                                sendToMail($value['email'], $msg_temp1['email_title'], $msg_temp1['email_content']);
                            }
    
                        }
                    }
                   
                   
                    // // 给客服人员发邮件
                    // if(!empty($value['email'])){
                    //     sendToMail($value['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请需要审核，详情请登录系统查看', '【315云防伪】亲爱的用户，您有设备保修激活申请需要审核，详情请登录系统查看.本邮件不用回复');
                    // }
                }
                $status = 1;
                $msg = "成功";
            }
        }

        return json(['status' => $status, 'msg' => $msg]);
    }
}
